<template>
  <div class="login-panel">
    <h1 class="title demo">后台管理系统</h1>
    <el-tabs v-model="currentTab" stretch type="border-card">
      <el-tab-pane name="account">
        <template #label>
          <!-- 在template中定义的标签将会是选项的标题 -->
          <span>
            <i class="el-icon-user-solid"></i>
            &nbsp;账号登录
          </span>
        </template>
        <login-account ref="accountRef" />
      </el-tab-pane>
      <el-tab-pane label="Config" name="phone">
        <template #label>
          <span>
            <i class="el-icon-mobile-phone"></i>
            &nbsp;手机登录
          </span>
        </template>
        <login-phone ref="phoneRef" />
      </el-tab-pane>
    </el-tabs>
    <div class="account-control">
      <el-checkbox v-model="isKeepPassword">记住密码</el-checkbox>
      <el-link type="primary" :underline="false">忘记密码</el-link>
    </div>
    <el-button class="login-btn" type="primary" @click="handleLoginClick">立即登录</el-button>
  </div>
</template>

<script setup lang="ts">
import LoginAccount from './login-account.vue'
import LoginPhone from './login-phone.vue'
import { ref } from '@vue/reactivity'

const isKeepPassword = ref(false)
// 在使用模板引用时，需使用官方定义的类型注解获取到指定组件实例暴露的类型
// 通过 typeof LoginAccount可以获取指定组件对象的类型，再通过InstanceType获取到实例暴露的属性的类型

type accountRefType = InstanceType<typeof LoginAccount> & ExposeAccount
type phoneRefType = InstanceType<typeof LoginPhone> & ExposePhone
const accountRef = ref<accountRefType>()
const phoneRef = ref<phoneRefType>()
const currentTab = ref<string>('account')

// 立即登录按钮事件
const handleLoginClick = () => {
  console.log(currentTab.value);
  if(currentTab.value === 'account'){
    // setup语法糖的坑，类型检测无法检测出loginAction，所以需要自定义类型
    accountRef.value?.loginAction(isKeepPassword.value)
  }else{
    phoneRef.value?.loginAction()
  }
}
</script>

<style scoped lang="less">
.login-panel {
  width: 400px;
  margin-bottom: 180px;
  .title {
    text-align: center;
    color: white;
    margin-bottom: 20px;
  }
  .el-tabs {
    box-shadow: 0px 0px 5px 1px #00000080;
    border-radius: 4px;
    display: flex;
    flex-direction: column;
  }
  .account-control {
    display: flex;
    justify-content: space-between;
  }
  .login-btn {
    display: flex;
    width: 100%;
    justify-content: center;
  }
}
</style>
